class ClientesController < ApplicationController

  respond_to :html, :json

  def index
    @clientes = Cliente.all
    respond_with @clientes
  end

  def show
    @cliente = Cliente.find(params[:id])
    respond_with @cliente
  end

  def new
    @cliente = Cliente.new
    respond_with @cliente
  end

  def edit
    @cliente = Cliente.find(params[:id])
  end

  def create
    @cliente = Cliente.new(params[:cliente])
    @cliente.ativo = false

    Cadastro.email_ativacao.deliver

    respond_to do |format|
      if @cliente.save
        format.html { redirect_to @cliente, notice: 'Cliente was successfully created.' }
        format.json { render json: @cliente, status: :created, location: @cliente }
      else
        format.html { render action: "new" }
        format.json { render json: @cliente.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @cliente = Cliente.find(params[:id])

    respond_to do |format|
      if @cliente.update_attributes(params[:cliente])
        format.html { redirect_to @cliente, notice: 'Cliente was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @cliente.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @cliente = Cliente.find(params[:id])
    @cliente.destroy

    respond_to do |format|
      format.html { redirect_to clientes_url }
      format.json { head :no_content }
    end
  end

  def aparelhos
    @aparelhos = Aparelho.find(:all, :conditions => ["cliente_id = ?", params[:id]], :include => :cliente)
    respond_with @aparelhos
  end
end
